<?php
class IndexAction extends MenuAction{
	/**
	 * @todo Show the Index page
	 */
	public function index(){
		$ContentThumb = M('ContentThumb');
		$ContentImage = M('ContentImage');
		$Channel = M('Channel');
		$Content = M('Content');
		$Playbill = M('Playbill');
		$thumb =$ContentThumb->order('thumb_id desc')->limit('9')->select();
		foreach ($thumb as $value) {
			$image = $Content->where('content_id='.$value['thumb_content'] .' AND content_state=1')->find();
			$thumbone[] = $image['content_title'];
			$thumbone[] = $value;
			$thumbs[] = $thumbone;
			unset($thumbone);
		}
		$somechannel = $Channel->where('channel_index=1 AND channel_state=1')
		->order('channel_sort ASC')->select();
		$infos = array();
		foreach ($somechannel as $key => $achannel) {
			$info = $Content->where('channel_id='.$achannel['channel_id'] .' AND content_state=1')->order('content_time desc')
			->limit($achannel['channel_index_count'])->select();
			$athumb = $ContentThumb->where('thumb_channelid='.$achannel['channel_id'])->order('thumb_id desc')->find();
			$smallimages[] = $athumb;
			$infos[] = $info;
		}
               
		$images = $ContentImage->order('image_id desc')->limit('20')->select();
		$displaybill = $Playbill->where('bill_show=1')->limit('7')->select();
		if($displaybill != ''){
			foreach ($displaybill as $key => $value) {
				$contentname = $Content->where("content_id=".$value["bill_content"] .' AND content_state=1')->find();
				$bill[] = $contentname["content_title"];
				$bill[] = $value;
				$bills[] = $bill;
				unset($bill);
			}
			$this->assign('showbanner','show');//控制是否显示banner
			$this->assign('bills',$bills);//输出bills banner的模板变量
		}
		$this->assign('images',$images);//缩略图的输出
		$this->assign('infos',$infos);
		$this->assign('thumbs',$thumbs);//left-pic位置输出9张图片
		$this->assign('smallimages',$smallimages); //每一个channel中的图片
		$this->display();
	}
	/**
	 * @todo Search a content %like%
	 */
	public function search(){
		$Content = M("Content");
		if(isset($_GET['keyWords']) && $_GET['keyWords'] != ""){
			if(isset($_GET['page'])){
				$nowpage=$_GET['page'];
			}else{
				$nowpage = 1;
			}
			$map['content_title']=array("like","%".$_GET['keyWords']."%");
			$count = $Content->where($map)->count();
			//每页显示的个数
			$number = 15;
			$count = ceil($count/$number);
			$low = ($nowpage-1) * $number;
			$searched = $Content->where($map)->limit($low.','.$number)->select();
			$down = $nowpage - 4;
			if($down<1)
			$down = 1;
			$up = $nowpage + 4;
			if($up > $count)
			$up = $count;
			//生成页码表示数组
			$middle = range($down, $up);
			//热点新闻和最新新闻
			$hots = $Content->where('content_state=1')->order('content_click_count desc')->limit('11')->select();
			$mostnews = $Content->where('content_state=1')->order('content_time desc')->limit('11')->select();
			$this->assign('mostnews',$mostnews);
			$this->assign('hots',$hots);
			$this->assign('keyWords',$_GET['keyWords']);
			$this->assign('count',$count);
			$this->assign('middle',$middle);
			$this->assign('nowpage',$nowpage);
			$this->assign('searched',$searched);
			$this->display();
		}else{
			$this->assign('waitSecond',10);
			$this->assign('jumpUrl','index');
			$this->error('请输入关键字之后再搜索');
		}
	}
}
?>